/*
 * timer-ss805x.h
 *
 * The timer driver for SS805X.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */

#ifndef __HAL_TIMER_SS805X_H__
#define __HAL_TIMER_SS805X_H__

#include "hal-config.h"
#include "timer-ss805x-pre.h"

/*
 * timerx micros list below, and x can be 0, 1, 2, 3, 4 etc.
 * 1. TX_CNT_START
 *    timerx count start
 * 2. TX_CNT_STOP
 *    timerx count stop
 * 3. TX_CNT_VAL_GET
 *    get timerx current count value
 * 4. TX_MAX_CNT_VAL_GET
 *    get timerx max count value
 * 5. TX_IS_OF
 *    check if timerx is overflow
 * 6. TX_OF_FLAG_CLR
 *    timerx overflow flag clr
 * 7. TX_RL_VAL_TO_OF_RATIO
 *    convert timerx reload val to overflow ratio
 * 8. TX_OF_RATIO_TO_RL_VAL
 *    convert timerx overflow ratio to reload val
 */
    /* timer0 */
    #define T0_CNT_START()                      (T0EN = 1)
    #define T0_CNT_STOP()                       (T0EN = 0)
    
    #define T0_CNT_VAL_GET()                    ((TH0 << 8) | TL0)

    #define T0_MAX_CNT_VAL_GET()                (65535)

    #define T0_IS_OF()                          (TF0 == 0)
    #define T0_OF_FLAG_CLR()                    (TF0 = 1)

    #define T0_RL_VAL_TO_OF_RATIO(relaod_val)   (                     \
                            SYS_SCLK_FREQ / TIMER0_DIV_VAL_PRE /      \
                            (T0_MAX_CNT_VAL_GET() - relaod_val))

    #define T0_OF_RATIO_TO_RL_VAL(freq)         (                     \
                            T0_MAX_CNT_VAL_GET() - SYS_SCLK_FREQ /    \
                            TIMER0_DIV_VAL_PRE / freq)   

    /* timer1 */
    #define T1_CNT_START()                      (T1EN = 1)
    #define T1_CNT_STOP()                       (T1EN = 0)
    
    #define T1_CNT_VAL_GET()                    ((TH1 << 8) | TL1)

    #define T1_MAX_CNT_VAL_GET()                (65535)

    #define T1_IS_OF()                          (TF1 == 0)
    #define T1_OF_FLAG_CLR()                    (TF1 = 1)

    #define T1_RL_VAL_TO_OF_RATIO(relaod_val)   (                   \
                            SYS_SCLK_FREQ / TIMER1_DIV_VAL_PRE /    \
                            (T1_MAX_CNT_VAL_GET() - relaod_val))

    #define T1_OF_RATIO_TO_RL_VAL(freq)         (                    \
                            T1_MAX_CNT_VAL_GET() - SYS_SCLK_FREQ /   \
                            TIMER1_DIV_VAL_PRE / freq)     

    /* timer2 */
    #define T2_CNT_START()                      (T2EN = 1)
    #define T2_CNT_STOP()                       (T2EN = 0)

    #define T2_CNT_VAL_GET()                    ((TH2 << 8) | TL2)

    #define T2_MAX_CNT_VAL_GET()                (65535)

    #define T2_IS_OF()                          (TF2 == 0)
    #define T2_OF_FLAG_CLR()                    (TF2 = 1)

    #define T2_RL_VAL_TO_OF_RATIO(relaod_val)   (                       \
                            SYS_SCLK_FREQ / TIMER2_DIV_VAL_PRE /        \
                            (TIMER2_MAX_CNT_VAL_GET() - relaod_val))

    #define T2_OF_RATIO_TO_RL_VAL(freq)         (                       \
                            TIMER2_MAX_CNT_VAL_GET() - SYS_SCLK_FREQ /  \
                            TIMER2_DIV_VAL_PRE / freq)    

#endif
